/*
 * Copyright 2010 Michael Nitsche
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.mnit.basis.daten.struktur.container;

import de.mnit.basis.daten.konstant.RECHT;
import de.mnit.basis.fehler.Fehler;

/**
 * @author Michael Nitsche
 */
public class Kiste<T> implements S_Kiste<T> {

	public  final RECHT recht;
	private T wert=null;
	private boolean leer=true;


	public Kiste(RECHT recht) {
		this.recht=recht;
	}

	public Kiste(RECHT recht, T wert) {
		this(recht);
		this.s(wert);
	}


	public static <T> Kiste<T> neu()					{ return new Kiste<T>(RECHT.LESEN); }
	public static <T> Kiste<T> neu(RECHT recht)			{ return new Kiste<T>(recht); }
	public static <T> Kiste<T> neu(RECHT recht, T init)	{ return new Kiste<T>(recht, init); }


	public synchronized void s(T t) {
		iSchreiben(this.recht,leer);
		wert=t;
		leer=false;
	}

	public synchronized T g() {
		iLesen(this.recht, leer);
		return wert;
	}

	public String toString() {
		String erg=this.getClass().getSimpleName(); //Klasse.gName(this);
		if(this.recht!=RECHT.OHNE)
			erg+="("+this.g()+")";
		return erg;
	}

	// INTERN

	private static final void iSchreiben(RECHT recht, boolean leer) {
		if(leer || recht==RECHT.SCHREIBEN)
			return;
		Fehler.sicherheit.da_Verboten("Feld ist schreibgeschützt!");
	}

	private static final void iLesen(RECHT recht, boolean leer) {
		if(recht!=RECHT.OHNE)
			return;
		Fehler.sicherheit.da_Verboten("Feld ist lesegeschützt!");
	}

}
